from Train import *
# 加载训练模型
rfc = joblib.load('FR_model2.joblib')

# 读取验证数据
test = pd.read_csv("test_2000_x.csv")
test = test.fillna(test.mean())

# 提取验证数据
# y1 = test["label"]
# 从第一行提取到最后一行
x1 = test.iloc[:, 1:]

predict_test = rfc.predict(x1)
# print("随机森林")
# print(metrics.classification_report(predict_test, y1))
print(predict_test)
print(type(predict_test))

# 构建预测结果字典
predictions = np.array(predict_test)
label_dict = {}
for index, label in enumerate(predictions):
    label_dict[index] = label

print(label_dict)

# 将字典转换为JSON字符串
def default_converter(obj):
    if isinstance(obj, np.int64):
        return int(obj)
    raise TypeError("Object of type {} is not JSON serializable".format(type(obj)))

data = label_dict
json_data = json.dumps(data, default=default_converter)

# 将JSON字符串写入文件
with open("data.json", "w") as json_file:
    json_file.write(json_data)

# 查看每个类别个数
from collections import Counter

my_dict = label_dict

value_counts = Counter(my_dict.values())

for value, count in value_counts.items():
    print(f'{value}: {count}')
# 这是一段测试代码git